﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CuTextBox : System.Web.UI.UserControl
{
    private bool enabled = true;
    private bool readOnly = false;
    private string width = "80";
    private string height = "15";
    private string toolTip;
    private string keyDownButton = "";
    private string validateText;
    private int textValueValidateID = -1;

    public bool Enabled
    {
        get { return enabled; }
        set { enabled = value; }
    }
    public bool ReadOnly
    {
        get { return readOnly; }
        set { readOnly = value; }
    }
    public string Width
    {
        get { return width; }
        set { width = value.ToString(); }
    }
    public string Height
    {
        get { return height; }
        set { height = value.ToString(); }
    }
    public string Text
    {
        get { return this.txtBox.Value; }
        set { this.txtBox.Value = value; }
    }
    public string ToolTip
    {
        get { return toolTip; }
        set { toolTip = value; }
    }
    public string KeyDownButton
    {
        get { return keyDownButton; }
        set { keyDownButton = value; }
    }
    public string ValidateText
    {
        get { return validateText; }
        set { validateText = value; }
    }
    public int TextValueValidateID
    {
        get { return textValueValidateID; }
        set { textValueValidateID = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    { }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        this.txtBox.Attributes.Add("title", this.toolTip);
        this.txtBox.Style.Add("Width", this.width + "px");
        this.txtBox.Style.Add("Height", this.height + "px");

        if (this.readOnly)
        {
            if (this.enabled)
            {
                this.txtBox.Style.Add("border-right", "0px solid");
                this.txtBox.Style.Add("border-left", "0px solid");
                this.txtBox.Style.Add("border-top", "0px solid");
                this.txtBox.Style.Add("border-bottom", "0px solid");
            }
            else
            {
                this.txtBox.Style.Add("border-right", "#7f9db9 1px solid");
                this.txtBox.Style.Add("border-left", "#7f9db9 1px solid");
                this.txtBox.Style.Add("border-top", "#7f9db9 1px solid");
                this.txtBox.Style.Add("border-bottom", "#7f9db9 1px solid");
            }

            this.txtBox.Attributes.Add("readonly", this.readOnly.ToString());
            this.txtBox.Attributes.Add("OnMouseOver", "this.style.cursor='default'");
        }
        else
        {
            this.txtBox.Attributes.Remove("readonly");
            this.txtBox.Style.Add("border-right", "#7f9db9 1px solid");
            this.txtBox.Style.Add("border-left", "#7f9db9 1px solid");
            this.txtBox.Style.Add("border-top", "#7f9db9 1px solid");
            this.txtBox.Style.Add("border-bottom", "#7f9db9 1px solid");
            this.txtBox.Attributes.Add("OnMouseOver", "this.style.cursor='text'");

            if (this.keyDownButton != "")
            {
                string[] strID = this.keyDownButton.Split(new char[] { '|' });

                if (strID != null && strID.Length > 0)
                {
                    this.txtBox.Attributes.Add("OnKeyDown", "ChildButtonSubmit(this.id, '" + strID[0].ToString() + "', '" + strID[1].ToString() + "')");
                }
            }
        }

        if (this.textValueValidateID != -1)
        {
            if (this.textValueValidateID == 0)//只能输入数字
            {
                this.txtBox.Attributes.Add("onkeyup", "value=value.replace(/[^\\d\\.-]/g,'')");
            }
            else if (this.textValueValidateID == 1)//只能输入字母或汉字
            {
                this.txtBox.Attributes.Add("onkeyup", "value=value.replace(/[^\u4E00-\u9FA5]/g,'')");
                this.txtBox.Attributes.Add("onbeforepaste", "clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))");
            }
            else if (this.textValueValidateID == 2)//只能输入电话号码
            {
                this.txtBox.Attributes.Add("onblur", "if(value.Trim() != '' && ! /(^[0-9]{3,4}-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^([0-9]{3,4})[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(value)) {alert('电话号码格式不正确！');this.value='';this.select();this.focus();}");
            }
            else if (this.textValueValidateID == 3)//只能输入传真号码
            {
                this.txtBox.Attributes.Add("onblur", "if(value.Trim() != '' && ! /(^[0-9]{3,4}-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^([0-9]{3,4})[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(value)) {alert('传真号码格式不正确！');this.value='';this.select();this.focus();}");
            }
            else if (this.textValueValidateID == 4)//只能输入Email
            {
                this.txtBox.Attributes.Add("onblur", "if(value.Trim() != '' && ! (new RegExp(/^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$/).test(value))) {alert('Email格式不正确！');this.value='';this.select();this.focus();}");
            }
            else if (this.textValueValidateID == 5)//只能输入邮编
            {
                this.txtBox.Attributes.Add("onblur", "if(value.Trim() != '' && ! /^[1-9]\\d{5}$/.test(value)) {alert('邮政编码格式不正确！');this.value='';this.select();this.focus();}");
            }
            else if (this.textValueValidateID == 6)//不能为空
            {
                this.txtBox.Attributes.Add("onblur","if(value.Trim() == ''){alert('当前项不能为空');this.select();this.focus();}");
            }
        }
    }
}
